package cn.kk.controller;

import cn.kk.base.model.BaseController;
import cn.kk.base.model.Result;
import cn.kk.model.auth.req.LoginReq;
import cn.kk.model.auth.req.RegisterReq;
import cn.kk.model.auth.vo.CaptchaVO;
import cn.kk.model.auth.vo.TokenAuthVO;
import cn.kk.service.AuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author kinoko
 */
@Tag(name = "认证服务")
@RestController
@RequestMapping("/auth")
public class AuthController extends BaseController {

    @Resource
    private AuthService authService;

    @GetMapping("/captcha")
    @Operation(summary = "验证码生成", description = "验证码生成")
    public Result<CaptchaVO> buildCaptcha() {
        return success(authService.buildCaptcha());
    }

    @PostMapping("/register")
    @Operation(summary = "新用户注册", description = "新用户注册")
    public Result<Object> register(@RequestBody @Validated RegisterReq req) {
        return toAjax(authService.register(req));
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录")
    public Result<TokenAuthVO> login(@RequestBody @Validated LoginReq loginInfo, HttpServletRequest request){
        return success(authService.login(loginInfo, request));
    }

    @GetMapping("/refresh")
    @Operation(summary = "刷新token", description = "刷新token")
    public Result<TokenAuthVO> refresh(){
        return success(authService.refresh());
    }
    
}